package com.foreveross.common.filter;
import java.io.IOException;  
  

import java.util.Map.Entry;

import javax.servlet.Filter;  
import javax.servlet.FilterChain;  
import javax.servlet.FilterConfig;  
import javax.servlet.ServletException;  
import javax.servlet.ServletRequest;  
import javax.servlet.ServletResponse;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.vo.SysUser;
import com.foreveross.vo.common.ResultVo;
  
  
/** 
 * @author hsy 
 * 
 */  
public class LoginFilter implements Filter {  
    private static final String SHOW_LOGIN_PATH = "SHOW_LOGIN_PATH";    //显示登录页面  
    private static final String WEIXIN_PATH = "WEIXIN_PATH";        //登录操作不能过滤掉
    private static final String COMMON_PATH = "COMMON_PATH";        //登录操作不能过滤掉
    
    private static final String MANAGE_PATH = "MANAGE_PATH";        //登录操作不能过滤掉
    private static final String LOGIN_PERSONID = "LOGIN_PERSONID";  //登录用户在session中的属性key -- session.setAttribute(key,value)
    private String showloginPath;  
    private String wixinPath;
    private String commonPath;
    private String managePath;
    private String loginPersonId;  
      
    public void init(FilterConfig config) throws ServletException {  
        showloginPath = config.getInitParameter(SHOW_LOGIN_PATH);  
        wixinPath = config.getInitParameter(WEIXIN_PATH);
        commonPath = config.getInitParameter(COMMON_PATH);
        managePath = config.getInitParameter(MANAGE_PATH);
        loginPersonId = config.getInitParameter(LOGIN_PERSONID);  
        if(showloginPath==null || showloginPath.equals("") || showloginPath.equals("null") ){  
            throw new ServletException("登录页面配置出错...");  
        }  
    }  
      
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  
         HttpServletRequest  httpReq  = (HttpServletRequest) request;  
         HttpServletResponse httpResp = (HttpServletResponse) response;  
         httpResp.setContentType("text/html");  
         String contentType = httpReq.getContentType();
         String path = httpReq.getContextPath();
         //判断是否是登陆页面  
         String servletPath = httpReq.getServletPath();  
         //flag:若为登陆页面的action路径 showloginPath/nologinPath,则赋值true，否则赋值false  
         if(servletPath.contains(showloginPath)||servletPath.contains(wixinPath) ||servletPath.contains(commonPath) || servletPath.contains(managePath)){
             chain.doFilter(request, response);  
             return;  
         }  
         else if(contentType != null && contentType.contains("application/x-www-form-urlencoded")){   
        	 //如果不是登录页面，则需先判断用户是否已登录  
        	 String userId = httpReq.getParameter(loginPersonId);
        	 SysUser sysUser=null;
        	 ResultVo vo=new ResultVo();
        	 sysUser = (SysUser) httpReq.getSession().getAttribute(userId); 

             if(sysUser != null){//如果不为空,则进行已登录处理  
            	 if(servletPath.contains("addOrUpdateHouseUser")){
            		for(Entry<String, String[]> em : httpReq.getParameterMap().entrySet()){
            			for(String str :em.getValue()){
            				System.out.println(em.getKey()+":"+str);
            			}            			
            		}
            	 }
                 chain.doFilter(request, response);  
             }else{ 
                	 vo.setSuccess(false);
                	 vo.setMessage("当前用户还未登陆或session失效！");
                	 vo.setCode(Constants.LOGIN_PARAMETER_ERROR);
                	 CommonUtil.PrintWriterResultVoWithJSON(httpResp, vo);
                	return;
             }  
         } else {
        	 chain.doFilter(request, response);  
             return;  
         }
    }  
      
    public void destroy(){  
        //do something  
    }  
      
}  
